package net.awpspace.mobile.management.house;

import java.util.Vector;

import javax.microedition.midlet.MIDlet;

import net.awpspace.mobile.form.BaseForm;
import net.awpspace.mobile.global.Utils;

import com.sun.lwuit.Button;
import com.sun.lwuit.ButtonGroup;
import com.sun.lwuit.Command;
import com.sun.lwuit.Container;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Label;
import com.sun.lwuit.RadioButton;
import com.sun.lwuit.TextArea;
import com.sun.lwuit.TextField;
import com.sun.lwuit.animations.CommonTransitions;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.geom.Dimension;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.layouts.FlowLayout;
import com.sun.lwuit.layouts.GridLayout;
import com.sun.lwuit.layouts.Layout;
import com.sun.lwuit.layouts.LayoutStyle;
import com.sun.lwuit.plaf.Border;
import com.sun.lwuit.spinner.Spinner;

public class AddMemberViewer extends BaseForm {

	private Command save;
	private Command reset;

	private TextField tname, ticard, taddress;
	private RadioButton male, female;
	private Spinner day, month, year;
	private Persistence per;
	
	public AddMemberViewer(MIDlet app, BaseForm parent) {
		super(app, parent);
		// TODO Auto-generated constructor stub
		this.setTitle("Thêm người");
		setTransitionInAnimator(CommonTransitions.createSlide(
				CommonTransitions.SLIDE_HORIZONTAL, true, 1200));
		setTransitionOutAnimator(CommonTransitions.createSlide(
				CommonTransitions.SLIDE_HORIZONTAL, true, 1200));
		per = Persistence.getInstance();
		creatForm();
		createCommand();
	}

	public void createCommand() {
		save = new Command("Save");
		reset = new Command("Reset");
		this.addCommand(save, 0);
		this.addCommand(reset, 1);
	}

	public void creatForm() {
		Container container = new Container(new BoxLayout(BoxLayout.Y_AXIS));
		Label infor = new Label("Thêm thông tin người trọ");
		Label lbname = new Label("Họ tên");
		tname = new TextField("Nguyễn Văn A");

		Container sex = new Container(new GridLayout(1, 2));
		Label lbsex = new Label("Giới tính\n");
		ButtonGroup groups = new ButtonGroup();
		male = new RadioButton();
		male.setText("Nam");
		male.setTextPosition(LEFT);
		male.setX(0);
		male.setSize(new Dimension(120, 30));
		female = new RadioButton();
		female.setText("Nữ");
		female.setX(male.getWidth());
		female.setTextPosition(LEFT);
		female.setSize(new Dimension(120, 30));
		groups.add(male);
		groups.add(female);
		sex.addComponent(male);
		sex.addComponent(female);

		Label lbdate = new Label("Ngày sinh");
		Container date = new Container(new GridLayout(1, 3));
		date.getStyle().setBorder(Border.createLineBorder(1), true);
		day = Spinner.create(1, 32, 2, 1);
//		day.getStyle().setBorder(Border.createLineBorder(1, 1000), true);
//		day.getStyle().setBgColor(0, true);
		month = Spinner.create(1, 13, 2, 1);
//		month.getStyle().setBorder(Border.createLineBorder(1, 1000), true);
		year = Spinner.create(1940, 2014, 1991, 1);
//		year.getStyle().setBorder(Border.createLineBorder(1, 1000), true);
		date.addComponent(day);
		date.addComponent(month);
		date.addComponent(year);

		Label lbicard = new Label("Chứng minh thư");
		ticard = new TextField("123456789");
		ticard.setConstraint(TextField.NUMERIC);
		ticard.setMaxSize(10);

		Label lbaddress = new Label("Địa chỉ");
		taddress = new TextField("Hà Nôi 3");

		container.addComponent(infor);
		container.addComponent(lbname);
		container.addComponent(tname);
		container.addComponent(lbsex);
		container.addComponent(sex);
		container.addComponent(lbicard);
		container.addComponent(ticard);
		container.addComponent(lbdate);
		container.addComponent(date);
		container.addComponent(lbaddress);
		container.addComponent(taddress);
		this.addComponent(container);

	}

	public void reset() {
		taddress.setText("");
		ticard.setText("");
		tname.setText("");

	}
	public void save(){
		if(per == null)
			System.out.println("NULLLLLLLLLLLLLL");
		Vector list = per.getData();
		if(list == null)
		System.out.println("LOIIIIIIIIIIIIIIIIIIIIIIi");
		int id;
		if (list == null)
			id = 1;
		else
		id = list.size() + 1;
		System.out.println(id);
		String name = tname.getText();
		String sex = "";
		System.out.println(name);
		if (male.isSelected())
			sex = "nam";
		else if (female.isSelected())
			sex = "nữ";
		System.out.println(sex);
		String d = String.valueOf(this.day.getSelectedItem());
		String m = String.valueOf(this.month.getSelectedItem());
		String y = String.valueOf(this.year.getSelectedItem());
		System.out.println(d + "/" + m + "/" + y);
		String card = ticard.getText();
		System.out.println(card);
		String add = taddress.getText();
		System.out.println(add);
		if (name.equals("") || add.equals("") || card.equals("") || sex.equals("")) {
			AudioManager.getInstance().play(AudioManager.FAILURE);
			Utils.getInstance().alert("Bạn hãy điền đầy đủ thông tin");
			return;
		}

		per.add(new HouseMember(id + "", name, sex, new AwpDate(d + "/" + m + "/" + y),
				card, add), true);
		AudioManager.getInstance().play(AudioManager.SUCCESS);
		Utils.getInstance().alert("Đã lưu thành công!");
	}
	
	public void reset1(){
		final Dialog log = new Dialog("Reset");
		log.setLayout(new BorderLayout());
		TextArea text = new TextArea();		
		text.setScrollVisible(true);
		text.setFocusable(true);
		text.setRows(25);						
		text.setText("Reset mọi dữ liệu sẽ bị hủy! Bạn có muốn reset không?");
		text.setEditable(false);
		log.addComponent(BorderLayout.CENTER, text);
		Container con = new Container(new GridLayout(1, 2));
		Button ok = new Button("Ok");
		ok.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				reset();
				log.dispose();
			}
		});
		Button dismiss = new Button("Dismiss");
		dismiss.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				AudioManager.getInstance().play(AudioManager.BUTTON);
				log.dispose();
			}
		});
		con.addComponent(ok);
		con.addComponent(dismiss);			
		log.addComponent(BorderLayout.SOUTH, con);
		log.show();
	}
	
	protected void processCommand(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getCommand().equals(save)) {

			save();
			
		} else if (e.getCommand().equals(reset)) {
			reset1();
		}
	}
}
